home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: Code loses this pointer value
- Date: 9 Jan 1996 20:02:06 +1100
- Organization: Werple Internet, Melbourne
- Message-ID: <4ctaue$c8l@werple.net.au>
- References: <1996Jan9.021817.24635@jarvis.cs.toronto.edu>
- NNTP-Posting-Host: werple.mira.net.au
-
- victorng@dgp.toronto.edu (Victor Ng-Thow-Hing) writes:
-
- >Hi everyone,
-
- >I have a particularly nasty bug in my C++ class code. Basically, the code
- >loses track of the this pointer value that points to the object that is
- >calling a member function. I have a class called bsplinevolume. The
- >member function "build_Controlpoints" calls another member function
- >"get_Skeleton_samp" as in the following short code snippet:
-
- >void bsplinevolume::build_Controlpoints()
- >{
- > <stuff deleted>
-
- > // Assign internal control points and interior cap ends.
- > double point[3];
- > for (int i=0; i<(N_s1-1); i++)
- > for (int j=0; j < N_s2; j++)
- > for (int k=0; k < N_s3; k++) {
- > get_Skeleton_samp(i,j,k,point); // member function
- > set_Controlpoint(i,j,k,point); // member function
- > }
-
- > <stuff deleted>
- >}
-
- >When I run dbx on this code, if I type: print *this while in
- >build_Controlpoints, I get the proper 'this' value:
-
- >(dbx) print *this
- >class bsplinevolume {
- > N1 = 0x10004858
- > N2 = 0x10004908
- > N3 = 0x100049c8
- > Axis = 0x10004398
- > N_axis = 0
- > Cap1 = 0x100044c0
- > N_cap1 = 0
- > Cap2 = 0x10004730
- > N_cap2 = 0
- > Skeleton_samps = 0x10004a78
- > N_s1 = 10
- > N_s2 = 10
- > N_s3 = 10
- > Controlpoints = 0x1000a840
- > N_cpts = 1000
- >}
-
- >NOW, when I print *this while in get_Skeleton_samp, I get:
-
- >(dbx) print *this
- ><no such address>
-
- >and consequently all the variables in this method are full of garbage.
- >You can see that 'this' has lost or corrupted its pointer value.
-
- >I'm using the CC SGI c++ compiler on an Indigo2 Extreme.
-
- >Does anyone have any experience with this bug?
-
- There does not appear to be enough information here to solve this
- problem. There is nothing apparent to me that is wrong with this code,
- but you haven't included the code for the function in which 'this'
- becomes garbage. Possibilities include:
- One of the functions you are calling is writing beyond the end
- of a variable it has a pointer to.
- Your print function is corrupting the object.
-
- Have a close look at all the writes in the functions you are calling. Try
- printing 'this' in lots of places (just the pointer value should do, not
- the whole object) to narrow it down. Print 'this' immediately before and
- after each function you call to isolate the function etc., etc.
- In most cases, a repeatable bug is not hard to find by a process of
- elimination. Keep in mind though, that each function gets its own copy of
- 'this', so just because its own 'this' is okay, it doesn't mean it hasn't
- corrupted the 'this' in a calling function.
-
- David White
- davidw@werple.mira.net.au
-
-
-